package servlet;

import util.Linksql;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserMessage extends HttpServlet {
    Connection con=null;
    PreparedStatement pre=null;
    String id=null;
    String sqlone="select * from student where stuid="+"?";
    String sqltwo="select * from teacher where teacherid="+"?";
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        try {
            id=request.getParameter("name");
            ResultSet reone=null;
            ResultSet retwo=null;
            con= Linksql.getConnection();
            pre= con.prepareStatement(sqlone);
            pre.setString(1,id);
            reone=pre.executeQuery();
            if(reone.next()){
                //do{
                    request.setAttribute("stuid",reone.getString("stuid"));
                    request.setAttribute("password",reone.getString("password"));
                    request.setAttribute("name",reone.getString("name"));
                    request.setAttribute("department",reone.getString("department"));
                    request.setAttribute("grade",reone.getString("grade"));
                    request.setAttribute("phone",reone.getString("phone"));
                    request.setAttribute("email",reone.getString("email"));
                    request.setAttribute("creditscore",reone.getInt("creditscore"));
                    request.getRequestDispatcher("/StudentMessage.jsp").forward(request,response);
                    response.sendRedirect("StudentMessage.jsp");
                //}while (reone.next());
                }else {
                pre=con.prepareStatement(sqltwo);
                pre.setString(1,id);
                retwo= pre.executeQuery();
                    if(retwo.next()){
                        //do{
                            request.setAttribute("teacherid",retwo.getString("teacherid"));
                            request.setAttribute("password",retwo.getString("password"));
                            request.setAttribute("name",retwo.getString("name"));
                            request.setAttribute("department",retwo.getString("department"));
                            request.setAttribute("title",retwo.getString("title"));
                            request.setAttribute("phone",retwo.getString("phone"));
                            request.setAttribute("email",retwo.getString("email"));
                            request.setAttribute("credit score",retwo.getInt("credit score"));
                            request.getRequestDispatcher("/TeacherMessage.jsp").forward(request,response);
                            response.sendRedirect("TeacherMessage.jsp");
                        //}while (retwo.next());
                    }else {
                        response.sendRedirect("MessageFalse.jsp");
                    }
                }
            con.close();
            pre.close();
            reone.close();
            retwo.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
